package com.right.health.app.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.formatter.IFillFormatter;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.UserApi;

/*
 * Created by hopeliao on 2019-09-24
 *
 */
public class ForgetPwdStep2Activity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final String TITLE_KEY = "title";
    private String mPhone;

    private EditText mPwdEt, mPwdConfirmEt;

    private ImageView mSwitchPwdVisibleImage, mSwitchConfirmPwdVisibleImage;

    private TextView mTitleTv;

    private boolean mPasswordConfirmVisible = false, mPasswordVisible = false;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.forget_pwd_step2);
    }

    @Override
    public void findView() {
        mPwdEt = (EditText) findViewById(R.id.et_pwd);
        mTitleTv = findViewById(R.id.tv_title);
        mSwitchPwdVisibleImage = (ImageView) findViewById(R.id.iv_pwd_change_visible);
        mSwitchPwdVisibleImage.setOnClickListener(this);

        mPwdConfirmEt = (EditText) findViewById(R.id.et_confirm_pwd);
        mSwitchConfirmPwdVisibleImage = (ImageView) findViewById(R.id.iv_pwd_confirm_change_visible);
        mSwitchConfirmPwdVisibleImage.setOnClickListener(this);

        findViewById(R.id.tv_confirm).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null ) {
            if (bundle.containsKey(BroadCastKey.DATA)) {
                mPhone = bundle.getString(BroadCastKey.DATA);
            }
            mTitleTv.setText(bundle.getString(TITLE_KEY));
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_pwd_change_visible) {
            mPasswordVisible = !mPasswordVisible;
            if (mPasswordVisible) {
                mPwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                mPwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mPwdEt.setSelection(mPwdEt.getText().length());
            mSwitchPwdVisibleImage.setImageResource(mPasswordVisible ?
                    R.mipmap.ic_pwd_change_visible2 :
                    R.mipmap.ic_pwd_change_visible1);
        } else if (v.getId() == R.id.iv_pwd_confirm_change_visible) {

            mPasswordConfirmVisible = !mPasswordConfirmVisible;
            if (mPasswordConfirmVisible) {
                mPwdConfirmEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                mPwdConfirmEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            mPwdConfirmEt.setSelection(mPwdConfirmEt.getText().length());
            mSwitchConfirmPwdVisibleImage.setImageResource(mPasswordConfirmVisible ?
                    R.mipmap.ic_pwd_change_visible2 :
                    R.mipmap.ic_pwd_change_visible1);
        } else if (v.getId() == R.id.tv_confirm) {
            if (TextUtils.isEmpty(mPhone)) {
                return;
            }
            String pwd1 = mPwdEt.getText().toString().trim();
            String pwdConfim = mPwdConfirmEt.getText().toString().trim();
            if (TextUtils.isEmpty(pwd1)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_pwd);
                return;
            }
            if (!pwd1.equals(pwdConfim)) {
                ToastUtil.show(App.getInstance(), R.string.please_enter_confirm_pwd);
                return;
            }

            mUserApi.resetPwd(mPhone, pwd1);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(App.getInstance(), response.error);
        } else {
            ToastUtil.show(App.getInstance(), "重置成功");
            finish();
        }
    }

    public static void launch(Context context, String title) {
        Intent intent = new Intent(context, ForgetPwdStep2Activity.class);
        if (!TextUtils.isEmpty(title)) {
            Bundle bundle = new Bundle();
            bundle.putString(TITLE_KEY, title);
            intent.putExtras(bundle);
        }

        context.startActivity(intent);
    }
}
